package com.william.thirdpart.qingyunke;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;
import java.util.Map;

/**
 * XianLiaoApi class
 *
 * @author william
 * @date 2021/1/12
 */
@Service
public class XianLiaoApi {
    @Autowired
    private RestTemplate restTemplate;
    public String process(String question) throws Exception{
        String url="http://api.qingyunke.com/api.php?key={key}&appid={appid}&msg={msg}";
        Map<String,String> urlMap= Maps.newHashMap();
        urlMap.put("key","free");
        urlMap.put("appid","0");
        urlMap.put("msg", URLEncoder.encode(question,"utf-8"));
        ResponseEntity<String> responseEntity=restTemplate.getForEntity(url,String.class,urlMap);
        XianLiaoResult result= JSON.parseObject(responseEntity.getBody(),XianLiaoResult.class);
        if(result.getResult().equals(NumberUtils.INTEGER_ZERO)){
            return result.getContent();
        }
        else
        {
            return StringUtils.EMPTY;
        }
    }
}
